package gk.example.adapter;

import gk.example.App;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.gkdate.R;

public class ChatAdapter extends BaseAdapter {
	Context mContext;
	ArrayList<String> mName;
	ArrayList<String> mMsg;
	ArrayList<String> mId;

	public ChatAdapter(Context context, ArrayList<String> name,
			ArrayList<String> msg, ArrayList<String> id) {
		mContext = context;
		mName = name;
		mMsg = msg;
		mId = id;
	}

	@Override
	public int getCount() {
		return mName.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ChatHolder chatHolder = null;
		if (convertView == null) {
			chatHolder = new ChatHolder();
			if (App.BmobUser.getObjectId().equals(mId.get(position))) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.item_chat_me, parent, false);
				chatHolder.tvName = (TextView) convertView
						.findViewById(R.id.tv_item_chat_me_name);
				chatHolder.tvMsg = (TextView) convertView
						.findViewById(R.id.tv_item_chat_me_msg);

				convertView.setTag(chatHolder);
			} else {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.item_chat_other, parent, false);
				chatHolder.tvName = (TextView) convertView
						.findViewById(R.id.tv_item_chat_other_name);
				chatHolder.tvMsg = (TextView) convertView
						.findViewById(R.id.tv_item_chat_other_msg);

				convertView.setTag(chatHolder);
			}

		} else {
			chatHolder = (ChatHolder) convertView.getTag();
		}
		chatHolder.tvName.setText(mName.get(position));
		chatHolder.tvMsg.setText(mMsg.get(position));

		return convertView;
	}
}

class ChatHolder {
	TextView tvName;
	TextView tvMsg;
}
